<?php

namespace app\common\model\salesman;

use app\common\model\custom\Category;
use app\common\utils\Utils;
use think\Model;
use traits\model\SoftDelete;

class Register extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'salesman_register';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'type_data'
    ];




    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function getTypeDataAttr($value, $data) {
        $value = $value ? $value : (isset($data['json_data']) ? $data['json_data'] : '');
        $arr = Utils::explodeStr($value, ',');
        $data = [];
        foreach ($arr as $k => $v) {
            $temp = Utils::explodeStr($v, '_');
            $data[$temp[0]] = $temp[1];
        }
        $cate_ids = array_keys($data);

        $values = Category::where(['id'=> ['in', $cate_ids]])->column('name', 'id');
        $retunData = [];
        $str = '';
        foreach ($values as $k => $v) {
            $retunData[$v] = $data[$k];
            $str .= $v . '-' . $data[$k] . ' ';
        }
        return ['arr' => $retunData, 'str' => $str];
    }

}
